home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 3006 / 3006.xpi / components / dhYTHQChecker.js < prev   
Text File  |  2010-01-15  |  8KB  |  250 lines

  1. /******************************************************************************
  2.  *            Copyright (c) 2006-2009 Michel Gutierrez. All Rights Reserved.
  3.  ******************************************************************************/
  4.  
  5. /**
  6.  * Constants.
  7.  */
  8.  
  9. const NS_YTHQCHECKER_CID = Components.ID("{4d140074-831e-456d-bb58-1c29ab1924b4}");
  10. const NS_YTHQCHECKER_PROG_ID = "@downloadhelper.net/ythq-checker;1";
  11. const DHNS = "http://downloadhelper.net/1.0#";
  12.  
  13. var Util=null;
  14.  
  15. /**
  16. * Object constructor
  17. */
  18. function YTHQChecker() {
  19.     try {
  20.         //dump("[YTHQChecker] constructor\n");
  21.         this.listMgr=Components.classes["@downloadhelper.net/media-list-manager"]
  22.                                         .getService(Components.interfaces.dhIMediaListMgr);
  23.         var prefService=Components.classes["@mozilla.org/preferences-service;1"]
  24.                                            .getService(Components.interfaces.nsIPrefService);
  25.         this.pref=prefService.getBranch("dwhelper.");
  26.         this.updateFormats();
  27.         this.ios = Components.classes["@mozilla.org/network/io-service;1"]
  28.                                          .getService(Components.interfaces.nsIIOService);
  29.         this.formatIndex=0;
  30.         this.done=false;
  31.     } catch(e) {
  32.         dump("[YTHQChecker] !!! constructor: "+e+"\n");
  33.     }
  34. }
  35.  
  36. YTHQChecker.prototype = {}
  37.  
  38. YTHQChecker.prototype.check=function(url,listener,args) {
  39.     //dump("[YTHQChecker] check("+url+")\n");
  40.     this.url=url;
  41.     this.listener=listener;
  42.     this.args=args;
  43.     this.checkNext();
  44. }
  45.  
  46. function YTHQRequestObserver(client) {
  47.     this.client=client;
  48. }
  49.  
  50. YTHQRequestObserver.prototype={
  51.  
  52.     onStartRequest: function(request,context) {
  53.     },
  54.  
  55.     onStopRequest: function(request,context) {
  56.         if(request.status==0) {
  57.             try {
  58.                 //dump("Got format "+this.client.formats[this.client.formatIndex]+"\n");
  59.                 var format=this.client.formats[this.client.formatIndex];
  60.                 this.client.listener.checkedYTHQ(this.client.url+"&fmt="+format,this.client.args,format,this.client.getExtension(format));
  61.             } catch(e) {
  62.                 dump("!!! [YTHQChecker] check callback failed: "+e+"\n");
  63.             }
  64.         } else {
  65.             this.client.formatIndex++;
  66.             this.client.checkNext();
  67.         }
  68.     }
  69. }
  70.  
  71. YTHQChecker.prototype.checkNext = function() {
  72.     if(this.formatIndex<this.formats.length) {
  73.         var uri=this.ios.newURI(this.url+"&fmt="+this.formats[this.formatIndex],null,null);
  74.         this.listMgr.addCurrentURL(uri.spec);
  75.         this.checker=Components.classes['@mozilla.org/network/urichecker;1'].createInstance(Components.interfaces.nsIURIChecker);
  76.         this.checker.init(uri);
  77.         this.checker.loadFlags = Components.interfaces.nsIRequest.LOAD_BYPASS_CACHE;
  78.         //dump("Testing format "+this.formats[this.formatIndex]+"\n");
  79.         this.checker.asyncCheck(new YTHQRequestObserver(this),this);
  80.     } else {
  81.         //dump("Give up checking hq\n");
  82.         try {
  83.             this.listener.checkedYTHQ(null,this.args,0,null);
  84.         } catch(e) {
  85.             dump("!!! [YTHQChecker] check callback failed: "+e+"\n");
  86.         }
  87.     }
  88. }
  89.  
  90. function YTHQMultiRequestObserver(client,format) {
  91.     this.client=client;
  92.     this.format=format;
  93. }
  94.  
  95. YTHQMultiRequestObserver.prototype={
  96.  
  97.     onStartRequest: function(request,context) {
  98.     },
  99.     onStopRequest: function(request,context) {
  100.         if(request.status==0) {
  101.             try {
  102.                 //dump("Multi Got format "+this.format+"\n");
  103.                 this.client.listener.checkedYTHQ(this.client.url+"&fmt="+this.format,this.client.args,this.format,this.client.getExtension(this.format));
  104.             } catch(e) {
  105.                 dump("!!! [YTHQChecker] check callback failed: "+e+"\n");
  106.             }
  107.         }
  108.     }
  109. }
  110.  
  111. YTHQChecker.prototype.checkMulti=function(url,listener,args) {
  112.     //dump("[YTHQChecker] checkMulti("+url+")\n");
  113.     try {
  114.     this.url=url;
  115.     this.listener=listener;
  116.     this.args=args;    
  117.     this.updateFormats();
  118.     for(var i in this.formats) {
  119.         var format=this.formats[i];
  120.         var uri=this.ios.newURI(this.url+"&fmt="+format,null,null);
  121.         this.listMgr.addCurrentURL(uri.spec);
  122.         this.checker=Components.classes['@mozilla.org/network/urichecker;1'].createInstance(Components.interfaces.nsIURIChecker);
  123.         this.checker.init(uri);
  124.         this.checker.loadFlags = Components.interfaces.nsIRequest.LOAD_BYPASS_CACHE;
  125.         //dump("Multi Testing format "+format+"\n");
  126.         this.checker.asyncCheck(new YTHQMultiRequestObserver(this,format),this);
  127.     }
  128.     } catch(e) { dump(e); }
  129. }
  130.  
  131. YTHQChecker.prototype.getExtension=function(format) {
  132.     if(format==13 || format==17)
  133.         return "3gp";
  134.     else if(format==18 || format==22 || format==37)
  135.         return "mp4";
  136.     else
  137.         return "flv";
  138. }
  139.  
  140. YTHQChecker.prototype.updateFormats=function() {
  141.     var formats=this.pref.getCharPref("ythq-formats").split(",");
  142.     this.formats=[];
  143.     for(var i in formats) {
  144.         if(formats[i].length>0) {
  145.             this.formats.push(parseInt(formats[i]));
  146.         }
  147.     }
  148. }
  149.  
  150. YTHQChecker.prototype.QueryInterface = function(iid) {
  151.     //dump("[YTHQChecker] QueryInterface("+iid+")\n");
  152.     if(
  153.         iid.equals(Components.interfaces.dhIYTHQChecker) ||
  154.         iid.equals(Components.interfaces.nsIStreamListener) ||
  155.         iid.equals(Components.interfaces.nsISupports)
  156.     ) {
  157.         return this;
  158.     }
  159.     throw Components.results.NS_ERROR_NO_INTERFACE;
  160. }
  161.  
  162. var vYTHQCheckerModule = {
  163.     firstTime: true,
  164.     
  165.     /*
  166.      * RegisterSelf is called at registration time (component installation
  167.      * or the only-until-release startup autoregistration) and is responsible
  168.      * for notifying the component manager of all components implemented in
  169.      * this module.  The fileSpec, location and type parameters are mostly
  170.      * opaque, and should be passed on to the registerComponent call
  171.      * unmolested.
  172.      */
  173.     registerSelf: function (compMgr, fileSpec, location, type) {
  174.  
  175.         if (this.firstTime) {
  176.             this.firstTime = false;
  177.             throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
  178.         }
  179.         compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  180.         compMgr.registerFactoryLocation(NS_YTHQCHECKER_CID,
  181.                                         "YTHQChecker",
  182.                                         NS_YTHQCHECKER_PROG_ID, 
  183.                                         fileSpec,
  184.                                         location,
  185.                                         type);
  186.     },
  187.  
  188.     unregisterSelf: function(compMgr, fileSpec, location) {
  189.         compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  190.         compMgr.unregisterFactoryLocation(NS_DH_YTHQCHECKER_CID, fileSpec);
  191.     },
  192.  
  193.     /*
  194.      * The GetClassObject method is responsible for producing Factory and
  195.      * SingletonFactory objects (the latter are specialized for services).
  196.      */
  197.     getClassObject: function (compMgr, cid, iid) {
  198.         if (!cid.equals(NS_YTHQCHECKER_CID)) {
  199.             throw Components.results.NS_ERROR_NO_INTERFACE;
  200.         }
  201.  
  202.         if (!iid.equals(Components.interfaces.nsIFactory)) {
  203.             throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  204.         }
  205.  
  206.         return this.vYTHQCheckerFactory;
  207.     },
  208.  
  209.     /* factory object */
  210.     vYTHQCheckerFactory: {
  211.         /*
  212.          * Construct an instance of the interface specified by iid, possibly
  213.          * aggregating it with the provided outer.  (If you don't know what
  214.          * aggregation is all about, you don't need to.  It reduces even the
  215.          * mightiest of XPCOM warriors to snivelling cowards.)
  216.          */
  217.         createInstance: function (outer, iid) {
  218.             if (outer != null) {
  219.                 throw Components.results.NS_ERROR_NO_AGGREGATION;
  220.             }
  221.     
  222.             if(Util==null) 
  223.                 Util=Components.classes["@downloadhelper.net/util-service;1"]
  224.                     .getService(Components.interfaces.dhIUtilService);
  225.  
  226.             return new YTHQChecker().QueryInterface(iid);
  227.         }
  228.     },
  229.  
  230.     /*
  231.      * The canUnload method signals that the component is about to be unloaded.
  232.      * C++ components can return false to indicate that they don't wish to be
  233.      * unloaded, but the return value from JS components' canUnload is ignored:
  234.      * mark-and-sweep will keep everything around until it's no longer in use,
  235.      * making unconditional ``unload'' safe.
  236.      *
  237.      * You still need to provide a (likely useless) canUnload method, though:
  238.      * it's part of the nsIModule interface contract, and the JS loader _will_
  239.      * call it.
  240.      */
  241.     canUnload: function(compMgr) {
  242.         return true;
  243.     }
  244. };
  245.  
  246. function NSGetModule(compMgr, fileSpec) {
  247.     return vYTHQCheckerModule;
  248. }
  249.  
  250.